package org.example.domain.model.factory;

import org.example.domain.model.repository.LoginTypeStrategeRepository;
import org.example.web.exception.ServerException;
import org.example.web.result.BaseErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class LoginTypeStrategeFactory {
    /**
     * Spring 通过自动注入
     * 将会自动收集所有类型匹配的 Bean，
     * 使用 Bean 名称作为键，Bean 实例作为值，将它们注入到 Map 中
     */
    @Autowired
    private Map<String, LoginTypeStrategeRepository> map;

    public LoginTypeStrategeRepository getLoginTypeStrategeRepository(String loginType) {
        LoginTypeStrategeRepository loginTypeStrategeRepository = map.get(loginType);
        if (loginTypeStrategeRepository == null) {
            throw new ServerException(BaseErrorCode.THE_LOGIN_MODE_IS_NOT_ENABLED);
        }
        return loginTypeStrategeRepository;
    }
}
